package com.servlet;

import com.service.UserService;
import com.util.Md5Util;
import com.util.SpringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;

@WebServlet("/register")
public class Register extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password1 = req.getParameter("password1");
        String password2 = req.getParameter("password2");
        String yzm = req.getParameter("yzm");

        PrintWriter writer = resp.getWriter();
        String verifyCode =(String) req.getSession().getAttribute("verifyCode");
        if (!yzm.equalsIgnoreCase(verifyCode)){
            writer.write("-1");
            return;
        }


        String md5Password1=null;
        String md5Password2=null;
        try {
            md5Password1 = Md5Util.toMd5(password1);
            md5Password2=Md5Util.toMd5(password2);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }



        UserService userService = SpringUtil.getBean(UserService.class);
        int i = userService.register(username, md5Password1, md5Password2,null);


        writer.write(i+"");
        writer.close();
    }
}
